home *** CD-ROM | disk | FTP | other *** search
- ; place N queens on a NxN board
-
- (array int boardx 20 boardy 20)
- (int nQueens N)
-
- (defun
- put-queen (int x y)(s)
- {
- (move-cursor (+ y 1) (+ 1 (* x 2)))(puts s)(update)
- }
- print-board
- {
- (move-cursor 0 0)
- (puts ".---------------.^M^J")
- (puts "|*|*|*|*|*|*|*|*|^M^J")
- (puts "|*|*|*|*|*|*|*|*|^M^J")
- (puts "|*|*|*|*|*|*|*|*|^M^J")
- (puts "|*|*|*|*|*|*|*|*|^M^J")
- (puts "|*|*|*|*|*|*|*|*|^M^J")
- (puts "|*|*|*|*|*|*|*|*|^M^J")
- (puts "|*|*|*|*|*|*|*|*|^M^J")
- (puts "|*|*|*|*|*|*|*|*|^M^J")
- (puts "`---------------'")
- }
- )
-
-
- (defun
- print-solution
- {
- (int j)
- (for (j 0)(< j N)(+= j 1) (msg "(" (boardx j) "," (boardy j) ")"))
- }
- threat (int a b x y)
- {
- (or
- (== a x)(== b y)
- (== (- a b)(- x y))
- (== (+ a b)(+ x y))
- )
- }
- conflict (int x y)
- {
- (int n)
- (for (n 0)(< n nQueens)(+= n 1)
- (if (threat x y (boardx n) (boardy n)) { TRUE (done) }))
- FALSE
- }
- fill-board (int x y)
- {
- (int i j z)(z x)
-
- (for (i z)(< i N)(+= i 1)
- {
- (for (j z)(< j N)(+= j 1)
- {
- (if (conflict i j) ()
- {
- (put-queen i j nQueens)
- (boardx nQueens i)(boardy nQueens j)(+= nQueens 1)
- (fill-board i j)
- (if (== nQueens N){ TRUE (done) })
- (-= nQueens 1)(put-queen i j ".")
- })
- })
- (z 0)
- })
- FALSE
- }
- Queens
- {
- (N (atoi (ask "Number of queens = ")))
- (nQueens 0)(print-board)
- (if (fill-board 0 0) (msg "done")
- (msg "No solution"))
- }
- )
- (Queens)
-